package com.ali.config;

import com.ali.common.PasswordEncryptType;
import com.ali.dao.UserAuthorityMapper;
import com.ali.dao.UserMapper;
import com.ali.dao.UserRoleMapper;
import com.ali.entity.User;
import com.ali.entity.UserAuthority;
import com.ali.entity.UserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 自定义数据库用户管理器
 */
@Component
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserAuthorityMapper userAuthorityMapper;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return user;
    }

    @Override
    public void createUser(UserDetails userDetails) {
        User user = new User();
        user.setUsername(userDetails.getUsername());
        user.setPassword(userDetails.getPassword().replace(PasswordEncryptType.BCRYPT, ""));
        user.setEnable(true);
        userMapper.insert(user);
    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    /**
     * 加载数据库用户信息
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        User user = userMapper.selectOne(userQueryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }

//        Collection<GrantedAuthority> authorities = getAuthorities(user);
//        //创建角色
//        return org.springframework.security.core.userdetails.User.withUsername(user.getUsername()).password(PasswordEncryptType.BCRYPT + user.getPassword()).authorities(authorities).build();

        String role = getRole(user);
        //创建角色
        return org.springframework.security.core.userdetails.User.withUsername(user.getUsername()).password(PasswordEncryptType.BCRYPT + user.getPassword()).roles(role).build();
    }

    /**
     * 获取用户权限
     */
    private Collection<GrantedAuthority> getAuthorities(User user) {
        Collection<GrantedAuthority> authorities = new ArrayList<>();

        QueryWrapper<UserAuthority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", user.getId());
        List<UserAuthority> userAuthorities = userAuthorityMapper.selectList(queryWrapper);

        for (UserAuthority userAuthority : userAuthorities) {
            authorities.add(new MyGrantedAuthority(userAuthority.getAuthority()));
        }
        return authorities;
    }

    /**
     * 获取用户角色
     */
    private String getRole(User user) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", user.getId());
        UserRole userRole = userRoleMapper.selectOne(queryWrapper);
        return userRole.getRole();
    }
}
